package com.hualan.sfzy.controller;

import com.hualan.sfzy.model.entity.ArticleClassify;
import com.hualan.sfzy.service.ArticleClassifyService;
import com.hualan.sfzy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

import static com.hualan.sfzy.constant.CommonConstant.*;

/**
 * 文章分类管理控制器
 */
@RestController
@RequestMapping("/articleClassify")
public class ArticleClassifyController {

    @Autowired
    private ArticleClassifyService articleClassifyService;

    /**
     * 添加文章分类
     */
    @PostMapping("/add")
    public Result add(@RequestBody ArticleClassify articleClassify) {
        int result = articleClassifyService.add(articleClassify);
        return result > 0 ? Result.success() : Result.error();
    }

    /**
     * 编辑文章分类
     */
    @PutMapping("/edit")
    public Result edit(@RequestBody ArticleClassify articleClassify) {
        int result = articleClassifyService.edit(articleClassify);
        return result > 0 ? Result.success() : Result.error();
    }

    /**
     * 删除文章分类
     */
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable Integer id) {
        int result = articleClassifyService.remove(id);
        return result > 0 ? Result.success() : Result.error();
    }

    /**
     * 批量删除文章分类
     */
    @DeleteMapping("/removeBatch")
    public Result removeBatch(@RequestBody Integer[] ids) {
        int result = articleClassifyService.removeBatch(ids);
        return result > 0 ? Result.success() : Result.error();
    }

    /**
     * 查询所有文章分类
     */
    @GetMapping("/find")
    public Result find() {
        List<ArticleClassify> articleClassifies = articleClassifyService.find();
        return Result.success(articleClassifies);
    }

    /**
     * 分页查询文章分类
     */
    @GetMapping("/findPage")
    public Result findPage(@RequestParam(value = PAGE_NUM, defaultValue = PAGE_NUM_DEFAULT) Integer pageNum,
                           @RequestParam(value = PAGE_SIZE, defaultValue = PAGE_SIZE_DEFAULT) Integer pageSize,
                          ArticleClassify articleClassify) {
        Map<String, Object> result = articleClassifyService.findPage(pageNum, pageSize, articleClassify);
        return Result.success(result);
    }

    /**
     * 根据ID查询文章分类
     */
    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable Integer id) {
        ArticleClassify articleClassify = articleClassifyService.findById(id);
        return Result.success(articleClassify);
    }
} 